package com.demoo.zjsu.filter;


import com.demoo.zjsu.dao.userinfo.UserDAO;
import com.demoo.zjsu.model.userinfo.Message;
import com.demoo.zjsu.model.userinfo.UserInfo;
import com.demoo.zjsu.string.userinfo.Code;
import com.demoo.zjsu.string.userinfo.Wechat;
import com.demoo.zjsu.util.HttpClientUtil.Client;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;

/**
 * Created by jshjsh on 2016/8/12.
 * 处理获取全部信息的模块
 */

@Service
public class CodeInterceptor implements HandlerInterceptor {


    @Autowired
    UserDAO dao;
    public boolean preHandle(HttpServletRequest httpServletRequest
            , HttpServletResponse httpServletResponse
            , Object o) throws Exception {
        HttpSession session = httpServletRequest.getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        if (userInfo == null) {
            String code = httpServletRequest.getParameter("code");
            String url = Code.GETACCESS
                    .replace(Wechat.APPID, Wechat.appId)
                    .replace(Code.CODE, code)
                    .replace(Code.SECRET, Wechat.appSecret);
            String result = Client.sendGet(url);
            Gson gson = new Gson();
            Message message = gson.fromJson(result, Message.class);
            String openId = message.getOpenid();
            String access_token = message.getAccess_token();
            String scope = message.getScope();
            if (message.getErrcode() != 0) {
                String uri = httpServletRequest.getRequestURL().toString();
                httpServletResponse.sendRedirect(uri);
                return false;
            }

            if (scope.equals(Wechat.SNSAPI_USERINFO)) {
                url = Code.GETINFO
                        .replace(Code.OPENID, openId)
                        .replace(Code.ACCESS_TOKEN, access_token);
                String user_info = Client.sendGet(url);
                userInfo = gson.fromJson(user_info, UserInfo.class);
                userInfo.setOpenId(openId);
                UserInfo userInfo1 = dao.getAllInfoByOpenId(userInfo);
                if (userInfo1 == null) {
                    String nickname = userInfo.getNickname().replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
                    userInfo.setNickname(nickname);
                    dao.insertUserInfo(userInfo);
                    session.setAttribute("user", userInfo);
                    return true;
                }
                session.setAttribute("user", userInfo1);
            } else if (scope.equals(Wechat.SNSAPI_BASE)) {
                UserInfo userInfo1 = dao.getBaseInfoByOpenId(message);
                if (userInfo1 == null) {
                    dao.insertOpenId(message);
                    int id = message.getId();
                    userInfo.setOpenId(openId);
                    userInfo.setId(id);
                    session.setAttribute("user", userInfo);
                    return true;
                }
                session.setAttribute("user", userInfo1);
            }
        }
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
